//
//  HomeView.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "HomeView.h"
#import "HomeMenuCell.h"
#import "SDCycleScrollView.h"

@interface HomeView()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIImageView *top_view;

@property (nonatomic, strong) UIView *user_info_view;

@property (nonatomic, strong) UIImageView *user_avatar_view;

@property (nonatomic, strong) UILabel *user_name_label;

@property (nonatomic, strong) UILabel *user_sign_label;

@property (nonatomic, strong) UICollectionView *menu_collection_view;

@property (nonatomic, strong) SDCycleScrollView *carousel_view;

@property (nonatomic, strong) UIButton *message_button;

@end

@implementation HomeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeTopBGView];
        
        [self makeUserInfoView];
        
        [self makeMenuCollectionView];
        
    }
    return self;
}

- (void)makeTopBGView
{
    CGFloat bgHeight = (SCREEN_HEIGHT - 20) * 0.7;
    
    _top_view = [UIImageView new];
    _top_view.userInteractionEnabled = YES;
    [self addSubview:_top_view];
    [_top_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(bgHeight);
    }];
    
    
    _carousel_view = [SDCycleScrollView new];
    
    _carousel_view.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    // cycleScrollView2.titlesGroup = titles;
    _carousel_view.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [_top_view addSubview:_carousel_view];
    
//    _carousel_view = [UIImageView new];
//    _carousel_view.image = [UIImage imageNamed:@"home_test_image"];
//    _carousel_view.backgroundColor = [UIColor yellowColor];
//    [_top_view addSubview:_carousel_view];
    [_carousel_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_top_view.mas_width).multipliedBy(0.5);
    }];
    
    _user_info_view = [UIView new];
    [_top_view addSubview:_user_info_view];
    [_user_info_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.left.mas_equalTo(_carousel_view.mas_right).mas_offset(0);
        make.height.mas_equalTo(bgHeight / 2);
    }];
    
}

- (void)loadCarouselData:(NSArray *)carouselData
{
    _carousel_view.imageURLStringsGroup = carouselData;
}

- (void)makeUserInfoView
{
    CGFloat bgHeight = (SCREEN_HEIGHT - 20) * 0.7;
    CGFloat avatarSize = bgHeight / 2 -40;
    
    _user_avatar_view  = [UIImageView new];
    _user_avatar_view.backgroundColor = [UIColor yellowColor];
    _user_avatar_view.layer.masksToBounds = YES;
    _user_avatar_view.layer.cornerRadius = (avatarSize) / 2;
    [_user_info_view addSubview:_user_avatar_view];
    [_user_avatar_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(_user_info_view);
        make.size.mas_equalTo(CGSizeMake(avatarSize, avatarSize));
    }];
    
    _user_name_label = [UILabel new];
    _user_name_label.font = [UIFont systemFontOfSize:17.0f];
    _user_name_label.textColor = RGB(255, 255, 255);
    [_user_info_view addSubview:_user_name_label];
    [_user_name_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_user_avatar_view.mas_centerY).mas_offset(-30);
        make.left.mas_equalTo(_user_avatar_view.mas_right).mas_offset(20);
        make.right.mas_equalTo(-60);
        make.height.mas_equalTo(20);
    }];
    
    _user_sign_label = [UILabel new];
    _user_sign_label.font = [UIFont systemFontOfSize:15.0f];
    _user_sign_label.textColor = RGB(255, 255, 255);
    [_user_info_view addSubview:_user_sign_label];
    [_user_sign_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_user_avatar_view.mas_centerY).mas_offset(10);
        make.left.mas_equalTo(_user_avatar_view.mas_right).mas_offset(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(20);
    }];
    
    _message_button = [UIButton new];
    [_message_button setImage:[UIImage imageNamed:@"home_message_button_image"] forState:UIControlStateNormal];
    [_message_button addTarget:self action:@selector(clickMessageButton:) forControlEvents:UIControlEventTouchUpInside];
    [_user_info_view addSubview:_message_button];
    [_message_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
}

- (void)clickMessageButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickMessage)]) {
        [self.delegate clickMessage];
    }
}

- (void)makeMenuCollectionView
{
    CGFloat itemWidth = (SCREEN_WIDTH - 20) / 4;
    CGFloat itemHeight = (SCREEN_HEIGHT - 20) - (SCREEN_HEIGHT - 20) * 0.7 - 4;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _menu_collection_view = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _menu_collection_view.backgroundColor = CLEARCOLOR;
    _menu_collection_view.dataSource = self;
    _menu_collection_view.delegate = self;
    [_menu_collection_view registerClass:[HomeMenuCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeMenuCell class])];
    [self addSubview:_menu_collection_view];
    [_menu_collection_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_top_view.mas_bottom).mas_offset(0);
        make.bottom.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
    }];
}

#pragma mark ```刷新用户数据```
- (void)refreshUserData:(UserModel *)userModel
{
    [_top_view sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,userModel.background]] placeholderImage:nil];
    [_user_avatar_view sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,userModel.avatar]] placeholderImage:nil];
    _user_name_label.text = userModel.username;
    _user_sign_label.text = userModel.sign;
}

- (void)setMenuData:(NSArray *)menuData
{
    _menuData = menuData;
}

#pragma mark ```collectionview datasource and delegate```
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _menuData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeMenuCell *menuCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeMenuCell class]) forIndexPath:indexPath];
    
    [menuCell loadHomeMenuCellData:(HomeMenuModel *)[_menuData objectAtIndex:indexPath.row]];
    
    return menuCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([self.delegate respondsToSelector:@selector(didSelectPushTask)]) {
            [self.delegate didSelectPushTask];
        }
    }else if (indexPath.row == 1){
        if ([self.delegate respondsToSelector:@selector(didSelectTaskList)]) {
            [self.delegate didSelectTaskList];
        }
    }else if (indexPath.row == 2){
        if ([self.delegate respondsToSelector:@selector(didSelectFriendList)]) {
            [self.delegate didSelectFriendList];
        }
    }else if (indexPath.row == 3){
        if ([self.delegate respondsToSelector:@selector(didSelectSetting)]) {
            [self.delegate didSelectSetting];
        }
    }
}



@end
